package com.kylin.common.taglib;

import java.io.IOException;
import java.util.Locale;

import javax.servlet.jsp.JspException;

import org.apache.struts.taglib.html.BaseHandlerTag;


/**
 * 生成 带日历的日期输入文本框 的标签类
 * @author lzh
 *
 */
public class CalendarInputTag  extends BaseHandlerTag{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/** 缺省值 */
	protected String value;

	/** 不使用property时定义的输入框名称 */
	protected String name;
	
	/** 接受actionform 的属性 */
	protected String property;

	/** 显示语言种类 */
	protected String language;

	protected String size;

	protected String id;

	protected String imgSrc;

	protected String imgId;

	
	/** struts的ActionForm 在配置文件中的formName*/
	protected String formName;

	/** 本页面ActionForm 的真实 classname  */
	protected String formClass;

	public int doStartTag() throws JspException {
		try {
			pageContext.getOut().println("<!-- \\ 日历输入控件 -->");
			pageContext.getOut().println(getInitScript());
			pageContext.getOut().println(getStartElement());
			pageContext.getOut().println(initCalendar());
		} catch (Exception e) {
			e.printStackTrace();
			throw new JspException(e);
		}
		return SKIP_BODY;
	}

	private String getInitScript(){
		if(pageContext.getAttribute("CalendarInitOK",pageContext.REQUEST_SCOPE) == null){
			try {
				pageContext.getOut().println("<!-- \\ 请注意本页面中不要重复定义以下脚本 -->");
			} catch (IOException e) {
				e.printStackTrace();
			}
			StringBuffer sb = new StringBuffer();
			sb.append("\n  <style media=\"all\" type=\"text/css\">@import url(\"/css/calendar-jos.css\");</style>");
			sb.append("\n    <script type=\"text/javascript\" src=\"/script/calendar.js\"></script>           ");
			sb.append("\n    <script type=\"text/javascript\" src=\"/script/calendar-setup.js\"></script>     ");
			sb.append("\n    <script type=\"text/javascript\" src=\""+getLanguageFile()+"\"></script>");
			pageContext.setAttribute("CalendarInitOK","OK",pageContext.REQUEST_SCOPE);
			return sb.toString();
		}else{
			return "";
		}
	}
	
	private String getLanguageFile(){
		String fileName = "";
		Locale locale = (Locale)pageContext.getAttribute(org.apache.struts.Globals.LOCALE_KEY,pageContext.SESSION_SCOPE);
		if(locale != null){
			fileName = "/script/lang/calendar-"+locale+".js";
		}else
			fileName = "/script/lang/calendar-default.js";
		return fileName;
	}
	private String getStartElement() throws JspException {
		StringBuffer sb = new StringBuffer("<input");
		sb.append(" name=\"").append(this.name).append("\"");
		if(this.id == null || this.id.equals(""))
			this.id = this.name;
		sb.append(" id=\"").append(this.id).append("\"");
		sb.append(" type=\"text\" readonly=\"readonly\" ");
		if (size != null) {
			sb.append(" size=\"").append(size).append("\"");
		}
        if(this.value != null){
			sb.append(" value=\"").append(value).append("\"");
        }
		sb.append(prepareEventHandlers());
		sb.append(prepareStyles());
		sb.append(">");
		sb.append("<img class=\"calendar\"");
		if(this.imgSrc == null || this.imgSrc.equals(""))
			this.imgSrc = "/images/calendar.png";
		sb.append("src=\""+imgSrc+"\" alt=\"calendar\" ");
		if(this.imgId == null || this.imgId.equals(""))
			this.imgId = "calendar_img";
		sb.append(" id=\""+imgId+"\" />");
		return sb.toString();
	}
	private String initCalendar(){
		StringBuffer sb = new StringBuffer();
		sb.append("\n<script type=\"text/javascript\">");
		sb.append("\n<!--");
		sb.append("\n    Calendar.setup(                        ");
		sb.append("\n	      {                                   ");
		sb.append("\n         inputField  : \""+this.id+"\",    ");
		sb.append("\n         ifFormat    : \"%Y-%m-%d\",       ");
		sb.append("\n         button      : \""+this.imgId+"\", ");
		sb.append("\n         align       : \"Tl\",             ");
		sb.append("\n         singleClick : true                ");
		sb.append("\n        }                                  ");
		sb.append("\n    );                                     ");
		sb.append("\n//--> ");
		sb.append("\n</script>");		
		return sb.toString();
	}

	public String getFormClass() {
		return formClass;
	}

	public void setFormClass(String formClass) {
		this.formClass = formClass;
	}

	public String getFormName() {
		return formName;
	}

	public void setFormName(String formName) {
		this.formName = formName;
	}

	public String getImgId() {
		return imgId;
	}

	public void setImgId(String imgId) {
		this.imgId = imgId;
	}

	public String getImgSrc() {
		return imgSrc;
	}

	public void setImgSrc(String imgSrc) {
		this.imgSrc = imgSrc;
	}

	public String getLanguage() {
		return language;
	}

	public void setLanguage(String language) {
		this.language = language;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProperty() {
		return property;
	}

	public void setProperty(String property) {
		this.property = property;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
}